﻿using DotNetty.Framework;
using iTool.Connector.ModelOfRequest;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;

namespace iTool.Connector.Message
{
    public abstract class BaseNotifyMessageParameter
    {
        /// <summary>
        /// 消息Token
        /// </summary>
        public string Token { get; set; }

        /// <summary>
        /// 接收方
        /// </summary>
        public string ReceiveChannel { get; set; }

        /// <summary>
        /// 消息[属性]参数（JSON）
        /// </summary>
        public string Parameter { get; set; }

        /// <summary>
        /// 消息内容
        /// </summary>
        //public string Content { get; set; }

        /// <summary>
        /// 是否是群组
        /// </summary>
        public bool IsGroup { get; set; }

        /// <summary>
        /// 发送者用户ID
        /// </summary>
        public string SendChannel { get; set; }

        /// <summary>
        /// 发送者的信息（JSON）
        /// </summary>
        public string SendChannelParameter { get; set; }

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime SendDateTime { get { return DateTime.Now; } }


        /// <summary>
        /// 获取字符串格式的通知数据
        /// </summary>
        /// <returns></returns>
        public virtual string GetNotifyMessageOfString<TMessage>(TMessage msg, string channel)
            where TMessage : AbstractMessage
            //where TClientInfo : ClientInfoAbstract
        {

            // 目前客户端发送消息需要做一层包装
            // 做兼容和规范处理

            var request = msg as QueryMessageRequest;
            //var clientInfo = client as ClientInfo;
            string result = string.Empty;

            // 构造当前消息数据
            {
                this.ReceiveChannel = request.ToChannel; //request.MessageKey;
                this.Token = request.Token;
                //this.Content = request.Content;
                this.Parameter = request.Parameter;
                this.SendChannel = channel;
                this.SendChannelParameter = string.Empty; // TODU
                this.IsGroup = request.IsGroup;
                msg = null;
                request = null;
            }

            // 转成JSON
            {
                // 定义时间格式
                IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

                IList<JsonConverter> convertList = new List<JsonConverter>();
                convertList.Add(timeFormat);

                // 小驼峰
                var serializerSettings = new JsonSerializerSettings
                {
                    // 设置为驼峰命名
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Converters = convertList
                };

                result = JsonConvert.SerializeObject(this, serializerSettings);

                timeFormat = null;
                convertList = null;
                serializerSettings = null;
            }

            return result;
        }
    }
}
